<?php
class SignUpController extends Model3_Controller
{
    public function init()
    {
        $this->view->title = 'Login';
    }

    public function indexAction()
    {
        $this->redirect('SignUp/login');
    }

    public function loginAction()
    {
        $msg = '';
        if($this->getRequest()->numParam() > 0)
        {
            $errorRequest = $this->getRequest()->getParam('error');
            switch($errorRequest)
            {
                case 0:
                    $msg = '';
                    break;
                case 1:
                    $msg = 'Usuario y contrase&ntilde;a no v&aacute;lidos';
                    break;
                case 2:
                    $msg = 'Usuario eliminado';
                    break;
                case 3:
                    $msg = 'Usuario desactivado';
                    break;
                default: $msg = 'Usuario y contrase&ntilde;a no v&aacute;lidos';
                    break;
            }

            $this->view->msg = $msg;
        }
    }



    protected function logoutAction()
    {
        Model3_Auth::deleteCredentials(); 
        if(isset($_SESSION['__M3']['Namespaces']['paginaInicial']['paginaInicial']))
        {
            $_SESSION['__M3']['Namespaces']['paginaInicial']['paginaInicial'] = '';
        }
        $welcome = new Model3_Session_Namespace('welcome');
        $welcome->first = null;
        
        header('Location: '.Class_Server::getFullHost());
    }

    protected function loginTryAction()
    {
        $server = new Class_Server();
        if($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            $helperUsers = new Helper_Users();
            $valid = $helperUsers->validateUser($post['username'], $post['password']);
            if($valid !== false)
            {
                if($valid === true)
                {
                    $credentials = Model3_Auth::getCredentials();
                    $session = new Model3_Session_Namespace('paginaInicial');
                    $this->redirect($session->paginaInicial);
                }
                elseif($valid == Helper_Users::ERROR_DELETED)
                {
                    $this->redirect('SignUp/index/1');
                }
                elseif($valid == Helper_Users::ERROR_BLOCKED)
                {
                    $this->redirect('SignUp/index/2');
                }
                else
                {
                    $this->redirect('SignUp/index');
                }
            }
        }
    }
    
    public function loginForGetAction()
    {
        
        $server = new Class_Server();
        $usuariosAdapter = MN_UsuariosTable::getInstance();
        
        if($this->getRequest()->getParams()>0)
        {
            $id = $this->getRequest()->getParam('id');
            $pass = $this->getRequest()->getParam('pass');
            
            $user = $usuariosAdapter->getUserById($id);
                    
            
            
            
            $helperUsers = new Helper_Users();
            $valid = $helperUsers->validateUser($user->username, $pass);
            
            
            
            if($valid !== false)
            {
                if($valid === true)
                {
                    $credentials = Model3_Auth::getCredentials();
                    $session = new Model3_Session_Namespace('paginaInicial');
                    $this->redirect('PetProfile/createPetProfile');
                    //$this->redirect($session->paginaInicial);
                }
                elseif($valid == Helper_Users::ERROR_DELETED)
                {
                    $this->redirect('SignUp/index/1');
                }
                elseif($valid == Helper_Users::ERROR_BLOCKED)
                {
                    $this->redirect('SignUp/index/2');
                }
                else
                {
                    $this->redirect('SignUp/index');
                }
            }
            else
            {
                
            }
        }
        
    }

}
